Bugfixes
Reduce code duplication