Refactoring of code

cleaning up and adding helper methodse, see #162