Code refactored to make it easier to follow. Merged documentation in readme.html