This is a lot of code for other people to debug for you. You might want to work on isolating each problem and getting them into the form of an SSCCE or MCVE.