Refactored, added probability calculation and bit encoding