almost final code