Added max level and cleaned up some code.