Added some tests. Light refactor and cleanup