Fixed issue #1 and cleaned up code a little