Organize Code

Better code optimization into functions