Chaging code structure, and fixing some random bugs