Draft code ready. Needs testing