Some bugs fixed, removed unused code.