Final modifications and optimizations for HW6