Fixed bug reported by issue #51 and #19 (thanks to johnnyPescarul)