Reorganize source file layout: introduce a package-level directory
Added unit tests, fixed bugs and API inconsistencies.