Ran some tests and improved the code