Removed some of the ugly stuff and made it more in line with the original code. I chose not to amend the commit on purpose.