Refactored, fixed bugs