some refactoring and optimization for level drawing