Improve style and add some functional tests