Slight refactoring and debugging