optimized code - removed redundancy