fixed bugs and combined all code