improved logic and syntax