Reduce code duplications and simplify API (Fixes #5)