fixed crash with default constructor, and some code refactored