Got rid of code duplication, where needed.
Refactoring and minor tweaks.