whole code completely broken into several functions