Fixed #30 with minor changes.