clean up code, fix logic in header