Small refactorings (similar to pappz' implementation)