Move reading stdin to separate function (tried to use C for this but it didn't work as well)