fixed issue #5 and #4... not fully tested yet