major refactoring. Was too much spaghetti