Refactoring. Removed a lot of redundant code.