code refactor to take into account bits per sample