Improve parsing logic