Fixes #24 and some improvements