Refactor code to make usage as python package easier.