Code optimized

Code optimized based on mhergers input