minor memory improvements, refactoring