After multiple modifications, with reduced hard-coding and code working