Refined code structure, filled in TODO details