Cleaned up the code... slightly
This is un-tested, so before any release, please test it