adds tests and refactors long method