Bug fixes

Fixes bug #28, #27, #13  and some minor issues.