Tweaked & refactored some code