code optimized and cleaned up unused vars