some refactoring, cleaned up code a lot