Fixed issue #5 and included a bunch of improvements