Improve code, consistency and add tests