refactor code and adjust the datatype to handle special cases