Cleaning up Petter's awful code