Added tests; minor refactor