cleaned up code a little bit more
major refactoring but still needs more refactoring