First working version finished. Refining and optimization needed (coming next).