Improve code logic