cleaned up things, split into functions, added smoothing