This is some fixes for issue #11 and #12

The first 3 commits is for issue #11
the 4th one is for issue #12
please review the code and merge if possible.