Minor refactoring. Improved comments and debug info.