fixed issues #17 and #5 + some cleanup