More or less done. Need to check with more testcases