fixed #4 + a partial rewrite