This match is rather huge, maybe some parts be broken in to more functions?