Clean up code a bit, was copied from Wiki source so not pretty