Removed unused code, fixed bugs