improved version of code