final code (hopefully)