Adding karma tests and tidying code