Cleaned up a bit and added a debug flag