Transferred similar code blocks in functions
Reformatted code [ci skip]