cleaned up the code a lot, refactored it a bit, minor changes to style