those long if blocks looks ugly, refactor will be nice