Total refactoring.

Code now is better structured, more readable, works faster, does not need separate script for writeng mbtiles.