Implemented Jezza's suggestions with a few modifications