merged refactoring from budcha