First functional version, TODO: some polishing and refining