made more robust, added tests